import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;


public class LibraryDriver {

	public static void main(String[] args) {
		try {
			Scanner scanfile = new Scanner(new File("plaintext.txt"));
			String text = "";
			while(scanfile.hasNext()) {
				text += scanfile.nextLine();
			}
			
			KeyGenerator keyGen = KeyGenerator.getInstance("AES");
			keyGen.init(128);
			SecretKey key = keyGen.generateKey();
			
			Cipher c = Cipher.getInstance("AES/CFB/PKCS5Padding");
			c.init(Cipher.ENCRYPT_MODE, key);
			String result = new String(c.doFinal(text.getBytes("utf-8")));
			IvParameterSpec ivSpec = new IvParameterSpec(c.getIV());
			System.out.println("Encrypted:\n" + result);
			c.init(Cipher.DECRYPT_MODE, key, ivSpec);
			result = new String(c.doFinal(result.getBytes()));
			System.out.println("\nDecrypted:\n" + result);
			
			key = keyGen.generateKey();
			c = Cipher.getInstance("AES/CBC/PKCS5Padding");
			c.init(Cipher.ENCRYPT_MODE, key);
			result = new String(c.doFinal(text.getBytes("utf-8")));
			ivSpec = new IvParameterSpec(c.getIV());
			System.out.println("\nEncrypted:\n" + result);
			c.init(Cipher.DECRYPT_MODE, key, ivSpec);
			result = new String(c.doFinal(result.getBytes()));
			System.out.println("\nDecrypted:\n" + result);
			
			
			/*
			 * 
			KeyGenerator keyGen = KeyGenerator.getInstance("AES");
			keyGen.init(128);
			SecretKey key = keyGen.generateKey();
			
			Cipher c = Cipher.getInstance("AES/CFB/PKCS5Padding");
			c.init(Cipher.ENCRYPT_MODE, key);
			String result = new String(c.doFinal(text.getBytes("utf-8")));
			
			IvParameterSpec ivSpec = new IvParameterSpec(c.getIV());
			c.init(Cipher.DECRYPT_MODE, key, ivSpec);
			result = new String(c.doFinal(result.getBytes()));
			 */
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
